package org.zhadoop.stream.cql.semanticanalyzer.parser.context;

import java.util.List;

import org.zhadoop.stream.api.opereators.JoinType;
import org.zhadoop.stream.cql.DriverContext;
import org.zhadoop.stream.cql.exception.CQLException;
import org.zhadoop.stream.cql.exception.SemanticAnalyzerException;
import org.zhadoop.stream.cql.hooks.SemanticAnalyzeHook;
import org.zhadoop.stream.cql.semanticanalyzer.SemanticAnalyzer;
import org.zhadoop.stream.cql.semanticanalyzer.parsecontextwalker.ParseContextWalker;
import org.zhadoop.stream.cql.tasks.Task;

/**
 * Join 右侧内容解析结果
 * 
 * @author zwd
 *
 */
public class JoinRigthBodyContext extends ParseContext {

	private JoinType joinType;

	private FromSourceContext rightStream;

	private OnConditionContext onCondition;

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(" " + getJoinOperator());
		sb.append(" " + rightStream.toString());
		if (onCondition != null) {
			sb.append(" ON " + onCondition.toString());
		}

		return sb.toString();
	}

	private String getJoinOperator() {
		switch (joinType) {
		case LEFT_OUTER_JOIN:
			return "LEFT OUTER JOIN";
		case RIGHT_OUTER_JOIN:
			return "RIGHT OUTER JOIN";
		case FULL_OUTER_JOIN:
			return "FULL OUTER JOIN";
		case NATURAL_JOIN:
			return "NATURAL JOIN";
		case CROSS_JOIN:
			return "CROSS JOIN";
		default:
			return "INNER JOIN";
		}
	}

	@Override
	public Task createTask(DriverContext driverContext, List<SemanticAnalyzeHook> analyzeHooks) throws CQLException {
		return null;
	}

	@Override
	public SemanticAnalyzer createAnalyzer() throws SemanticAnalyzerException {
		return null;
	}

	@Override
	protected void walkChild(ParseContextWalker walker) {
		walkExpression(walker, onCondition);
		walkExpression(walker, rightStream);
	}

	public JoinType getJoinType() {
		return joinType;
	}

	public void setJoinType(JoinType joinType) {
		this.joinType = joinType;
	}

	public FromSourceContext getRightStream() {
		return rightStream;
	}

	public void setRightStream(FromSourceContext rightStream) {
		this.rightStream = rightStream;
	}

	public OnConditionContext getOnCondition() {
		return onCondition;
	}

	public void setOnCondition(OnConditionContext onCondition) {
		this.onCondition = onCondition;
	}
}
